import json

from firstDjango.decorators import post
from index.protocol.book_protocol import BookUpdateInfo, Book
from index.service import books_service
from django.core.handlers.wsgi import WSGIRequest
from django.http.response import JsonResponse


# 添加书
@post(request_data_class=Book)
def add_book(request: WSGIRequest, data: Book) -> JsonResponse:
    return books_service.add_book(data)


# 删除书信息
def delete_book(request: WSGIRequest):
    data = json.loads(request.body.decode("utf-8"))
    book_id = data.get("book_id")
    books_service.delete_book(book_id)
    return "ok"


# 修改书信息
@post(request_data_class=BookUpdateInfo)
def update_book_info(request: WSGIRequest, book: BookUpdateInfo):
    books_service.update_book_info(book)
    return "ok"


# 通过书编号查询书信息
def getBook_by_number(request: WSGIRequest) -> JsonResponse:
    book_number = request.GET.get("number")
    book_info = books_service.getBook_by_number(book_number)
    return book_info


# 通过书名查询书
def getBook_by_title(request: WSGIRequest) -> JsonResponse:
    book_title = request.GET.get("title")
    resp = books_service.getBook_by_title(book_title)
    return JsonResponse(resp)


# 添加出版社
def add_pub(request: WSGIRequest) -> JsonResponse:
    pub = json.loads(request.body.decode('utf-8'))
    pubname = pub.get("pubname")
    rsp = books_service.add_pub(pubname)
    return JsonResponse(rsp)


# 删除出版社
def delete_pub(request: WSGIRequest) -> JsonResponse:
    data = json.loads(request.body.decode("utf-8"))
    pub_id = data.get("pub_id")
    rsp = books_service.delete_pub(pub_id)
    return JsonResponse(rsp)


# 添加作者

def add_author(request: WSGIRequest) -> JsonResponse:
    author = json.loads(request.body.decode("utf-8"))
    number = author.get("number")
    name = author.get("name")
    company = author.get("company")
    email = author.get("email")
    rsp = books_service.add_author(number, name, company, email)
    return JsonResponse(rsp)


# 删除作者
def delete_author(request: WSGIRequest) -> JsonResponse:
    data = json.loads(request.body.decode("utf-8"))
    author_id = data.get("author_id")
    rsp = books_service.delete_author(author_id)
    return JsonResponse(rsp)


# 修改作者
def update_author(request: WSGIRequest) -> JsonResponse:
    author = json.loads(request.body.decode("utf-8"))
    author_id = author.get("author_id")
    number = author.get("number")
    name = author.get("name")
    company = author.get("company")
    email = author.get("email")
    rsp = books_service.update_author(author_id, number, name, company, email)
    return JsonResponse(rsp)


def fetch_books(req: WSGIRequest):
    page = int(req.GET.get('page', 1))
    page_size = int(req.GET.get('page_size', 20))
    order_by = req.GET.get('order_by', 'pub_date')
    desc = bool(int(req.GET.get('desc', 1)))
    user = req.session.get('user')
    book_id = int(req.GET.get('id', 0))
    title = req.GET.get('title')
    author = req.GET.get('author')

    total_count, books = books_service.fetch_books(
        req, user, page, page_size, order_by, desc, book_id=book_id, title=title, author=author
    )

    return {
        "page": page,
        "page_size": page_size,
        "total_count": total_count,
        "books": books
    }
